package com.tgd.gbb.uikit.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tgd.gbb.uikit.R;

import java.util.ArrayList;
import java.util.Calendar;


public class WLQQHeightPicker extends LinearLayout {
    public static final String PICKED_TIME_EXT = "picked_time";
    private static final int UPDATE_TITLE_MSG = 0x111;
    private static final int UPDATE_WHEEL = 0x112;
    private static final int UPDATE_UpdateDay_MSG = 0x113;
    //private TextView mPickerTitle;
    private WheelView mWheelHeight;
    private TextView mCancelBtn;
    private TextView mConfirmBtn;

    private int height;
    private int mHeight;

    private CallBack mCallBack;

    private Calendar mCalendar;
    private int mDefaultDayWhellIndex = 0;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATE_TITLE_MSG: {
                    updateTitle();
                }
                break;
                case UPDATE_WHEEL: {
                    updateWheel();
                }
                break;
            }

        }
    };
    private WheelView.OnSelectListener mYearListener = new WheelView.OnSelectListener() {
        @Override
        public void endSelect(int id, String text) {
            height = 100 + id;
            mHandler.sendEmptyMessage(UPDATE_TITLE_MSG);
        }

        @Override
        public void selecting(int id, String text) {
        }
    };

    private Activity mContext;

    public WLQQHeightPicker(Context context) {
        this(context, null);
    }

    public WLQQHeightPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mContext = (Activity) getContext();
        LayoutInflater.from(mContext).inflate(R.layout.weight_picker, this);
        //mPickerTitle = (TextView) findViewById(R.id.picker_title);
        mWheelHeight = (WheelView) findViewById(R.id.weight);
        mCancelBtn = (TextView) findViewById(R.id.cancel);
        mConfirmBtn = (TextView) findViewById(R.id.confirm);
        mWheelHeight.setOnSelectListener(mYearListener);
        mConfirmBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mCallBack.ConfirmAndGetData(height);
            }
        });
        mCancelBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //mContext.finish();
                mCallBack.Cancel();
            }
        });
    }


    /**
     * set WLQQTimePicker date
     *
     * @param height
     */
    public void setDate(int height) {
        this.height = height;
        mWheelHeight.setData(getYearData());

        mHandler.sendEmptyMessage(UPDATE_TITLE_MSG);
        mHandler.sendEmptyMessage(UPDATE_WHEEL);
    }

    private void updateTitle() {
        //mPickerTitle.setText(mContext.getString(R.string.picker_title, mYear, mMonth + 1, mDay, mHour));
    }

    private void updateWheel() {
        mWheelHeight.setDefault(height - 100);
    }

    private ArrayList<String> getYearData() {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 100; i < 201; i++) {
            list.add(i + "cm");
        }
        return list;
    }

    public interface CallBack {
        public void ConfirmAndGetData(int height);

        public void Cancel();
    }

    public void init(CallBack mCallBack) {
        this.mCallBack = mCallBack;
    }
}